package com.nf.health.app.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nf.health.app.R;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.models.DeviceData;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainDataAdapter extends BaseAdapter {

    private Activity context;
    private List<DeviceData> datas;

    public MainDataAdapter(Activity context, List<DeviceData> datas) {
        this.context = context;
        this.datas = datas;
    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获得数据对象
        DeviceData data = datas.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_data, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //设置图标
        Glide.with(context)
                .load(data.getDeviceImage())
                .placeholder(R.drawable.default_image)
                .into(holder.ivIcon);
        //设置数据类型
        holder.tvType.setText(data.getDeviceName());
        //设置具体数据
        holder.tvData.setText(data.getDeviceData().getDataItem());
        //设置时间
        if (data.getDeviceTime() == null || data.getDeviceTime().length() < 18){
//            holder.tvTime.setText("00:00");
        }else{
            holder.tvTime.setText(data.getDeviceTime().substring(5,11));
        }

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.iv_icon)
        CircleImageView ivIcon;
        @Bind(R.id.tv_type)
        TextView tvType;
        @Bind(R.id.tv_data)
        TextView tvData;
        @Bind(R.id.tv_time)
        TextView tvTime;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

//    /**
//     * 以逗号把字符串分行显示
//     * @param line
//     * @return
//     */
//    private String getStringItem(String line){
//        StringBuilder sb = new StringBuilder();
//        String lines[] = line.split(",");
//        for (int i = 0; i < lines.length; i++) {
//            sb.append(lines[i]);
//            sb.append("\r\n");//换行符
//        }
//        return sb.toString();
//    }
}
